package com.liliudong.pinkyponk.api.admin.controller.sys;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liliudong.pinkyponk.biz.base.BaseController;
import com.liliudong.pinkyponk.biz.sys.convert.SysMenuConvert;
import com.liliudong.pinkyponk.biz.sys.entity.SysMenu;
import com.liliudong.pinkyponk.biz.sys.request.SysMenuRequest;
import com.liliudong.pinkyponk.biz.sys.response.SysMenuResponse;
import com.liliudong.pinkyponk.biz.sys.service.SysMenuService;
import com.liliudong.pinkyponk.core.util.R;
import com.liliudong.pinkyponk.core.validator.Create;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Menu controller
 *
 * @author liliudong
 * @date 2022-07-04 15:14
 **/
@RestController
@RequestMapping("/v1/sys/menu")
public class SysMenuController extends
        BaseController<SysMenu, SysMenuRequest, SysMenuResponse, SysMenuConvert, SysMenuService> {

    public SysMenuController(SysMenuService service) {
        super(SysMenuConvert.INSTANCE, service);
    }

    /**
     * create
     *
     * @param sysMenuRequest
     * @return
     */
    @PostMapping
    @PreAuthorize("hasAuthority('sys:menu:create')")
    public ResponseEntity<?> create(
            @RequestBody @Validated(Create.class) SysMenuRequest sysMenuRequest) {
        SysMenu sysMenu = reqToEntity(sysMenuRequest);
        service.save(sysMenu);
        return ok(sysMenu);
    }

    /**
     * deleteById
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @PreAuthorize("hasAuthority('sys:menu:delete')")
    public ResponseEntity<?> deleteById(@PathVariable Integer id) {
        service.removeById(id);
        return ok();
    }

    /**
     * modifyById
     *
     * @param sysMenuRequest
     * @return
     */
    @PutMapping
    @PreAuthorize("hasAuthority('sys:menu:modify')")
    public ResponseEntity<?> modifyById(@RequestBody SysMenuRequest sysMenuRequest) {
        SysMenu sysMenu = reqToEntity(sysMenuRequest);
        service.updateById(sysMenu);
        return ok(sysMenu);
    }

    /**
     * selectById
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @PreAuthorize("hasAuthority('sys:menu:info')")
    public ResponseEntity<?> selectById(@PathVariable Integer id) {
        SysMenu sysMenu = service.getById(id);
        return ok(sysMenu);
    }

    /**
     * selectList
     *
     * @return
     */
    @GetMapping("/list")
    @PreAuthorize("hasAuthority('sys:menu:list')")
    public ResponseEntity<?> selectList(@RequestParam(defaultValue = "1") Long current,
                                        @RequestParam(defaultValue = "10") Long size) {
        Page<SysMenu> pageQuery = getPageQuery(current, size);
        Page<SysMenu> pageResult = service.page(pageQuery);
        return ok(pageResult);
    }

    /**
     * selectByParentId
     *
     * @param parentId
     * @return
     */
    @GetMapping("/{parentId}/parentId")
    @PreAuthorize("hasAuthority('sys:menu:list')")
    public ResponseEntity<?> selectByParentId(@PathVariable Integer parentId) {
        List<SysMenu> sysMenus = service.list(
                Wrappers.<SysMenu>lambdaQuery().eq(SysMenu::getParentId, parentId));
        return ok(sysMenus);
    }

    /**
     * selectMenus
     *
     * @return
     */
    @PreAuthorize("isAuthenticated()")
    @GetMapping("/menus")
    public ResponseEntity<?> selectMenus() {
        List<SysMenu> sysMenus = service.listExcludeButton();
        return R.ok(sysMenus);
    }
}
